package com.tobeface.wechat.message;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

/**
 * @author loudyn
 */
public final class WeChatMessage {

    private final WeChatMessageVerb verb;
    private final Map<String, Object> payloads;

    /**
     * @param builder
     */
    WeChatMessage(WeChatMessageBuilder builder) {
        this.verb = builder.verb;
        this.payloads = ImmutableMap.copyOf(builder.payloads);
    }

    /**
     * @return
     */
    public String getSender() {
        return (String) getPayloads().get(WeChatMessagePayloads.SENDER_PAYLOAD);
    }

    /**
     * @return
     */
    public String getReceiver() {
        return (String) getPayloads().get(WeChatMessagePayloads.RECEIVER_PAYLOAD);
    }

    /**
     * @return
     */
    public String getCreateTime() {
        return (String) getPayloads().get(WeChatMessagePayloads.CREATETIME_PAYLOAD);
    }

    /**
     * @return
     */
    public WeChatMessageVerb getVerb() {
        return verb;
    }

    /**
     * @return
     */
    public Map<String, Object> getPayloads() {

        return payloads;
    }

    /**
     * @return
     */
    public boolean isEmptyPayloads() {
        return getPayloads().isEmpty();
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getVerb(), getPayloads());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (null == obj || getClass() != obj.getClass()) {
            return false;
        }

        WeChatMessage other = (WeChatMessage) obj;
        return Objects.equal(getVerb(), other.getVerb()) && Objects.equal(getPayloads(), other.getPayloads());
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("verb", getVerb()).add("payloads", getPayloads()).toString();
    }
}
